var _staticHtml={};
$(function () {
    xmglTableViewHandler();

    _staticHtml.dialogDiv = $("#dialog_div").html();
    _staticHtml.dialogDiv2 = $("#dialog_div2").html();
    $("#dialog_div").remove();
    $("#dialog_div2").remove();
});

$(window).resize(function () {
    $("#xmglTable").bootstrapTable('resetView');
});

function xmglTableViewHandler() {
    var searchVal = $('#searchInput').val();

    $("#xmglTable").bootstrapTable('destroy').bootstrapTable({
        method:"POST",
        dataType:"json",
        url:"xmgl/queryXmglTable",
        contentType:"application/x-www-form-urlencoded",
        //查询参数,每次调用是会带上这个参数，可自定义
        queryParams: function(params)
        {
            var param = {};
            param["searchVal"] = searchVal;
            param["pageSize"] = params.pageSize;
            param["pageNumber"] = params.pageNumber;
            return param;
        },
        onClickRow: function (row, element, field) {
            $('.info').removeClass('info');//去除之前选中的行的，选中样式
            $(element).addClass('info');//添加当前选中的 success样式用于区别
        },
        striped: true,                      //是否显示行间隔色
        cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
        pagination: true,                   //是否显示分页（*）
        sortable: false,                     //是否启用排序
        sortOrder: "asc",                   //排序方式
        queryParamsType : "undefined",
        search: false,      //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
        strictSearch: false,
        showColumns: false,     //是否显示所有的列
        showRefresh: false,     //是否显示刷新按钮
        uniqueId: "xmdm",      //每一行的唯一标识，一般为主键列
        sortName:"xmdm",
        showToggle:false,     //是否显示详细视图和列表视图的切换按钮
        dataLocale:"zh-CN",
        sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
        pageNumber: 1,                       //初始化加载第一页，默认第一页
        pageSize: 10,                       //每页的记录行数（*）
        pageList: [10, 20, 50],        //可供选择的每页的行数（*）
        height: $(document).height() - 85,
        columns: [
            {
                field: 'xmmc',
                title: '项目名称',
                align:'center'
            }, {
                field: 'xmdm',
                title: '项目代码',
                align:'center'
            }, {
                field: 'xmms',
                title: '项目描述',
                align:'center'
            }, {
                field: 'xmdm',
                title: '操作',
                align:'center',
                formatter: function (value, row, index) {
                    var returnStr = '<span class="text-primary" onclick="openSjjModal(this)" xmdm="'+value+'" xmmc="'+row.xmmc+'" style="margin:0 10px; cursor: pointer; color: #337ab7;">数据集</span>';
                    returnStr += '<span class="text-primary" onclick="editXM(this)" xmdm="'+value+'" xmmc="'+row.xmmc+'" xmms="'+row.xmms+'" style="margin:0 10px; cursor: pointer; color: #337ab7;">修改</span>';
                    returnStr += '<span class="text-primary" onclick="delXM(this)" xmdm="'+value+'" style="margin:0 10px; cursor: pointer; color: #337ab7;">删除</span>';
                    return returnStr;
                }
            }
        ]
    });
}

function editXM(obj){
    var addflag = false;
    var title = '修改项目';
    var xmdmVal = $(obj).attr("xmdm");
    var xmmcVal = $(obj).attr("xmmc");
    var xmmsVal = $(obj).attr("xmms");
    if(xmdmVal == undefined || xmdmVal == ''){
        xmdmVal = '';
        xmmcVal = '';
        xmmsVal = '';
        addflag = true;
        title = '新建项目';
    }
    var html = '<div style="padding: 0 20px;">' +
        '<input id="add_flag" type="hidden" value="' + (addflag ? 1 : 0) + '"/>'+
        '<input id="xmdm" type="hidden" value="' + xmdmVal + '"/>' +
        '<input id="xmmc" type="hidden" value="' + xmmcVal + '"/>' +
        '<input id="xmms" type="hidden" value="' + xmmsVal + '"/>' +
        '<script type="text/javascript" src="platcommon/layer-wn.js?v=1.0.0"></script>' +
        '<script src="psms-xmgl-dialog.js"></script>' +
        _staticHtml.dialogDiv + '</div>';

    var index = parent.layer.open({
        type: 1,
        title: title,
        area: ['400px', '300px'],
        shadeClose: false, //点击遮罩关闭
        scrollbar: true,//禁止浏览器滚动
        maxmin: false,
        content: html,
        end: function () {
            xmglTableViewHandler();
        }
    });
}

function delXM(obj) {
    var xmdmVal = $(obj).attr("xmdm");
    LayerWn.showConfirm("确认删除项目代码为" + xmdmVal + "的记录?", function () {
        $.ajax({
            type: "POST",
            url: "xmgl/delXM",
            data: {"xmdm": xmdmVal},
            dataType: "json",
            success: function (data) {
                if (data.code == 1) {
                    LayerWn.showSucAlert("删除成功");
                    xmglTableViewHandler();
                }
            }
        });
    });
}

function openSjjModal(obj){
    var xmdmVal = $(obj).attr("xmdm");
    var xmmcVal = $(obj).attr("xmmc");
    var html = '<div style="padding: 0 20px;">' +
        '<input id="xmdm" type="hidden" value="' + xmdmVal + '"/>' +
        '<input id="xmmc" type="hidden" value="' + xmmcVal + '"/>' +
        '<link rel="stylesheet" href="../../../../../../sjzc-common/src/main/resources/static/css/plugins/bootstrap-table/bootstrap-table.min.css">' +
        '<link rel="stylesheet" href="../../../../../../sjzc-common/src/main/resources/static/css/metroStyle/metroStyle.css">' +
        '<script type="text/javascript" src="plugins/bootstrap-table/bootstrap-table.min.js"></script>' +
        '<script type="text/javascript" src="plugins/bootstrap-table/bootstrap-table-mobile.min.js"></script>' +
        '<script type="text/javascript" src="plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>' +
        '<script type="text/javascript" src="js/jquery.ztree.core.js"></script>' +
        '<script type="text/javascript" src="js/jquery.ztree.exhide.js"></script>' +
        '<script type="text/javascript" src="platcommon/layer-wn.js?v=1.0.0"></script>' +
        '<script src="psms-xmgl-dialog.js"></script>' +
        _staticHtml.dialogDiv2 + '</div>';

    var index = parent.layer.open({
        type: 1,
        title: xmmcVal+'项目数据集管理',
        area: ['1150px', '500px'],
        shadeClose: false, //点击遮罩关闭
        scrollbar: true,//禁止浏览器滚动
        maxmin: false,
        content: html,
        end: function () {

        }
    });
}